'use strict';

/**
 * @name UserListCtrl
 * @author ZYF
 */

angular.module('metalabOperationApp').controller('UserListCtrl', ['$scope', '$rootScope', '$state', '$stateParams', 'MetaResources', 'MetaRemind', 'MetaFilter', 'MetaSearch', 'MetaSort', 'MetaUtils', function($scope, $rootScope, $state, $stateParams, MetaResources, MetaRemind, MetaFilter, MetaSearch, MetaSort, MetaUtils) {
    $scope.listCtrl = {
        filterConfig: ['area_choice', 'talent', 'province'],
        filterParams: {},
        searchConfig: {
            'name_like': '姓名',
            'partnerInfo.organization_like': '机构',
            'partnerInfo.post_like': '职务',
            'mobile_like': '手机',
        },
        searchParams: {},
        sortParams: '-created_at',
        tableParams: {}
    };
    $scope.init = function() {
        MetaUtils.initListCtrl($scope, $stateParams);
    };
    $scope.getListData = function(page) {
        var queryParams = {
            '_embed': 'talent',
            '_sort': '',
            '_range': '',
            'partnerInfo.areas': '',
            'partnerInfo.camp': $stateParams.province,
        };
        var dynamicQueryParams = ['talent'];
        MetaUtils.buildQueryParams(page, queryParams, dynamicQueryParams, $scope.listCtrl, $stateParams);
        MetaResources.user.query(
            queryParams,
            function(data, header) {
                MetaUtils.receiveListData($scope.listCtrl, $stateParams, page, data, header);
                angular.forEach(data, function(value) {
                    if (value.partnerInfo !== undefined) {
                        value.partnerInfo.areas = value.partnerInfo.areas === undefined ? '暂无' : value.partnerInfo.areas.join(',');
                    } else {
                        value.partnerInfo = {
                            areas: '暂无'
                        };
                    }
                    value.research = false;
                    if (value.tags !== undefined) {
                        for (var i = 0; i < value.tags.length; i++) {
                            if (value.tags[i] === "科研人员") {
                                value.research = true;
                            }
                        }
                    }
                    var processKey = {
                        'demand': {
                            query: 'tags_not',
                            method: 'demand'
                        },
                        'technology': {
                            query: 'tags_not',
                            method: 'technology'
                        },
                        'cooperation': {
                            query: 'tags',
                            method: 'demand'
                        },
                        'solution': {
                            query: 'tags',
                            method: 'technology'
                        }
                    };
                    angular.forEach(processKey, function(innerValue, key) {
                        queryParams = {
                            '_range': 0,
                            'publisher_id': value.id,
                            'version': 'online'
                        };
                        queryParams[innerValue.query] = '合作';
                        MetaResources[innerValue.method].query(queryParams,
                            function(data, header) {
                                value[key] = header()['x-count'];
                            },
                            function(error) {
                                MetaRemind.serverError(error);
                            });
                    });
                });
            },
            function(error) {
                MetaRemind.serverError(error);
            });
    };
    $scope.changeUrl = function() {
        var urlParams = {
            page: $scope.listCtrl.tableParams.currentPage,
            sort: $scope.listCtrl.sortParams,
            showAll: $scope.listCtrl.tableParams.isShowAll
        };
        MetaFilter.encodeURLFilterParams(urlParams, $scope.listCtrl.filterParams);
        MetaSearch.encodeUrlSearchParams(urlParams, $scope.listCtrl.searchParams);
        $state.go('meta.permission.user.user.list', urlParams);
    };
    $scope.init();
}]);
